package com.example.q_kang.hhh.order;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.order.match.BuyDakuanMatchDetailActivity;
import com.example.q_kang.hhh.view.SelectPictureDialog;
import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.base.MVPActivity;
import com.sskj.base.base.App;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.ImageUtil;
import com.sskj.base.view.ToolBarLayout;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/04/16
 */
public class ComplainActivity extends MVPActivity<ComplainPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.iv_pay_img)
    ImageView ivPayImg;
    @BindView(R.id.btn_upload_pay_img)
    Button btnUploadPayImg;
    @BindView(R.id.btn_confirm_pay)
    Button btnConfirmPay;
    private String order;
    private String mImagePath;
    private String payImg;
    private SelectPictureDialog selectPictureDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_complain;
    }

    @Override
    public ComplainPresenter getPresenter() {
        return new ComplainPresenter();
    }

    @Override
    public void initView() {
        order = getIntent().getStringExtra("order");
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, btnUploadPayImg, view -> {
            selectPic();
        });
        ClickUtil.click(300, btnConfirmPay, view -> {
            if (TextUtils.isEmpty(payImg)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaComplainActivity2));
                return;
            }
            mPresenter.uploadJia(order, payImg);
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadImg(new File(mImagePath));
        }
    }

    public void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(ComplainActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(ComplainActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }


    @Override
    public void initData() {

    }

    public static void start(Context context, String order) {
        Intent intent = new Intent(context, ComplainActivity.class);
        intent.putExtra("order", order);
        context.startActivity(intent);
    }

    public void uploadSuccess(String src) {
        this.payImg = src;
        ImageUtil.setImage(this, payImg, ivPayImg);
    }
}
